//
//  OfficalViewController.m
//  GolfFriend
//
//  Created by vescky on 14-2-3.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "OfficalViewController.h"
#import "OfficalCell.h"
#import "AppsEngine.h"
#import "GFVOParser.h"
#import "DXAlertView.h"
#import "CacheDaoForfficalNews.h"
#import "OfficalNewsDetailViewController.h"
#import "AppDelegate.h"

@interface OfficalViewController () <UITableViewDataSource,UITableViewDelegate> {
    NSMutableArray *officalNewsArray;
}

@end

@implementation OfficalViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Private
- (void)initData {
    //读取缓存数据，再请求网上数据
    NSMutableArray *cDataArray = [[CacheDaoForfficalNews shareCacheDaoForfficalNews] getCacheDataLimitRow:NSMakeRange(0, 20) newsType:0];
    if (cDataArray && [cDataArray count] > 0) {
        if (officalNewsArray) {
            [officalNewsArray removeAllObjects];
        }
        officalNewsArray = cDataArray;
    }
    
    //网上请求数据
    [[AppsEngine shareAppsEngine] submitRequest:@"gfGetOfficalNews" param:nil onCompletion:^(id jsonResponse) {
        
        if ([jsonResponse objectForKey:@"status"] && [[jsonResponse objectForKey:@"status"] isEqualToString:@"success"]) {
            NSLog(@"刷新成功");
            NSMutableArray *arr1 = [[GFVOParser shareParser] paseDataForOfficalNewsContentArray:jsonResponse];
            if (!arr1 || [arr1 count] <= 0) {
                NSLog(@"没有数据了...");
            }
            else {
                [officalNewsArray removeAllObjects];
                officalNewsArray = arr1;
                [[CacheDaoForfficalNews shareCacheDaoForfficalNews] updateCacheDataWithNewsContentArray:officalNewsArray];
                [self refreshTableView];
            }
        }
    } onError:^(NSError *error) {
        NSLog(@"联网失败");
//        DXAlertView *dAlert = [[DXAlertView alloc] initWithTitle:@"温馨提示" contentText:@"暂无网络连接!" leftButtonTitle:nil rightButtonTitle:@"确定"];
//        [dAlert show];
    } method:@"GET"];
}

- (void)refreshTableView {
    if (officalNewsArray && [officalNewsArray count] > 0) {
//        CGRect tbRect = tbView.frame;
//        tbRect.size.height = [self getTableViewHeightWithDataArray:officalNewsArray];
//        [tbView setFrame:tbRect];
//        [noDataView removeFromSuperview];
        [tbView reloadData];
        [nodataView setHidden:YES];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [officalNewsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"OfficalNews";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(!cell){
        cell = [[[NSBundle mainBundle] loadNibNamed:@"OfficalCell" owner:self options:nil] objectAtIndex:0];
    }
    cell.tag = indexPath.row;//区分奇偶
    if([cell respondsToSelector:@selector(setCellDataInfo:)]){
        NSDictionary *newsDict = [officalNewsArray objectAtIndex:indexPath.row];
        [cell performSelector:@selector(setCellDataInfo:) withObject:newsDict];
    }
    
    [cell setTag:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 35.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tbView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"--- current selected index:%d",indexPath.row);
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[officalNewsArray objectAtIndex:indexPath.row]];
    OfficalNewsDetailViewController *newDetailVc = [[OfficalNewsDetailViewController alloc] init];
    newDetailVc.newsInfo = dict;
    [[APP_DELEGATE navigationController] pushViewController:newDetailVc animated:YES];
//    [self.navigationController pushViewController:newDetailVc animated:YES];
}

@end
